home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / telecomm / 295 / uniterm.20d / ezgem.c < prev    next >
C/C++ Source or Header  |  1991-07-06  |  5KB  |  210 lines

  1. /* EZGEM
  2.    This program converts GEM files generated by UniTerm into the
  3.    format wanted by EasyDraw.  The changes include rescaling and
  4.    joining vectors into polylines.
  5.    Don Rice / FXDDR@ALASKA.bitnet / CIS 72337,3417
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10.  
  11. #define XSCALE    6
  12. #define XOFFS    2730
  13. #define YSCALE    5
  14. #define YOFFS    3276
  15.  
  16. short    inbuf[256], outbuf[256] =
  17. {
  18. #include <gemhead.h>
  19. };
  20.  
  21. short    inptr, outptr;
  22.  
  23. struct
  24. {
  25.     short op, vert, parms, subop, x[128], y[128], p[128];
  26. } incode;
  27.  
  28. short    newx[128], newy[128], nxy;
  29. short    nvecs, nlines, nreads, nwrites;
  30.  
  31. short    startline[5] = { 5, 0, 1, 99, 80 },
  32.     polyfill[17] = { 5, 0, 2, 99, 155, 1, 5, 0, 2, 99, 157, 0, 23,
  33.       0, 1, 99, 0 },
  34.     polyline[4] = { 6, 2, 0, 99 },
  35.     endline[5] = { 5, 0, 1, 99, 81 };
  36. main()
  37. {
  38.     int old, new;
  39.     short getmeta(), op;
  40.     char fname[80];
  41.  
  42.     printf( "Name of UniTerm GEM file: " );
  43.     gets( fname );
  44.     if( (old = open( fname, O_RDONLY | O_RAW )) == -1 )
  45.         terminate( "Unable to open input file." );
  46.     printf( "Name of new GEM file: " );
  47.     gets( fname );
  48.     if( (new = open( fname, O_WRONLY | O_CREAT | O_RAW )) == -1 )
  49.         terminate( "Unable to open output file." );
  50.  
  51.     init_files( old, new );
  52.     while( (op = getmeta( old )) != 6 ) ;    /* Skip old header */
  53.     wrvect( new );
  54.     while( (op = getmeta( old )) != 4 )
  55.     {
  56.         if( op == -1 ) break;    /* EOF */
  57.         if( op == 6 ) wrvect( new );
  58.     }
  59.     close( old );
  60.     finish( new );
  61.     close( new );
  62.     printf( "\n%d vectors read; %d polylines written.\n", nvecs, nlines );
  63.     terminate( "File conversion completed." );
  64. }    /* main */
  65.  
  66. cpyvert( new )    /* Copy vertices to polyline list */
  67. int new;
  68. {
  69.     short i, j;
  70.  
  71.     j = (nxy ? nxy-1 : 0 );
  72.     for( i=0; i<incode.vert; i++ )
  73.     {
  74.         if( j > 127 )
  75.         {
  76.             wrpoly( new );
  77.             newx[0] = newx[127];
  78.             newy[0] = newy[127];
  79.             j = 1;
  80.         }
  81.         newx[j] = incode.x[i];
  82.         newy[j++] = incode.y[i];
  83.     }
  84.     nxy = j;
  85. }    /* cpyvert */
  86.  
  87. finish( new )    /* Flush output buffer, write end of file */
  88. int new;
  89. {
  90.     if( nxy > 0 ) wrpoly( new );
  91.     wrshort( new, -1 );
  92.     wrshort( new, 0 );
  93.     wrshort( new, 0 );
  94.     wrshort( new, 0 );
  95.     if( outptr > 0 ) write( new, outbuf, 512 );
  96. }    /* finish */
  97.  
  98. short getmeta( old )    /* Read new metafile operation */
  99. int old;
  100. {
  101.     short i, getshort();
  102.  
  103.     incode.op = getshort( old );
  104.     if( incode.op == -1 ) return( -1 );
  105.     incode.vert = getshort( old );
  106.     incode.parms = getshort( old );
  107.     incode.subop = getshort( old );
  108.     for( i=0; i<incode.vert; i++ )
  109.     {
  110.         incode.x[i] = getshort( old );
  111.         incode.y[i] = getshort( old );
  112.     }
  113.     for( i=0; i<incode.parms; i++ ) incode.p[i] = getshort( old );
  114.     return( incode.op );
  115. }    /* getmeta */
  116.  
  117. short getshort( old )
  118. int old;
  119. {
  120.     short x, l;
  121.  
  122.     x = ((inbuf[inptr]>>8)&0x0FF) | (inbuf[inptr]<<8);
  123.     if( ++inptr >= 256 )
  124.     {
  125.         l = read( old, inbuf, 512 );
  126.         printf( "%dK read, %dK written\r", (++nreads)/2, nwrites/2 );
  127.         while( l < 512 )
  128.             if( l >= 0 ) inbuf[l++] = -1;
  129.             else l=0;
  130.         inptr = 0;
  131.     }
  132.     return( x );
  133. }    /* getshort */
  134.  
  135. init_files( old, new )    /* Set up i/o buffers */
  136. int old, new;
  137. {
  138.     read( old, inbuf, 512 );
  139.     inptr = 16;
  140.     outptr = 172;
  141.     nxy = nwrites = nvecs = nlines = 0;
  142.     nreads = 1;
  143. }    /* init_files */
  144.  
  145. terminate( str )
  146. char *str;
  147. {
  148.     char ans[80];
  149.  
  150.     puts( str );
  151.     printf( "Press RETURN to quit: " );
  152.     gets( ans );
  153.     exit();
  154. }    /* terminate */
  155.  
  156. wrpoly( new )    /* Write polyline to output */
  157. int new;
  158. {
  159.     short i;
  160.  
  161.     polyline[1] = nxy;
  162.     for( i=0; i<5; i++ ) wrshort( new, startline[i] );
  163.     if( nxy > 2 )    /* true polyline */
  164.         for( i=0; i<17; i++ ) wrshort( new, polyfill[i] );
  165.     for( i=0; i<4; i++ ) wrshort( new, polyline[i] );
  166.     for( i=0; i<nxy; i++ )
  167.     {
  168.         wrshort( new, newx[i] );
  169.         wrshort( new, newy[i] );
  170.     }
  171.     for( i=0; i<5; i++ ) wrshort( new, endline[i] );
  172.     nlines++;
  173.     nxy = 0;
  174. }    /* wrpoly */
  175.  
  176. wrshort( new, i )    /* Write short int to output */
  177. int new;
  178. short i;
  179. {
  180.     i = ((i>>8)&0x0FF) | (i<<8);    /* Swap byte order */
  181.     outbuf[outptr++] = i;
  182.     if( outptr >= 256 )
  183.     {
  184.         write( new, outbuf, 512 );
  185.         printf( "%dK read, %dK written\r", nreads/2, (++nwrites)/2 );
  186.         outptr = 0;
  187.     }
  188. }    /* wrshort */
  189.  
  190. wrvect( new )    /* Add vector to polyline buffer */
  191. int new;
  192. {
  193.     short i;
  194.  
  195.     for( i=0; i<incode.vert; i++ )
  196.     {
  197.         incode.x[i] = incode.x[i]/XSCALE - XOFFS;
  198.         incode.y[i] = incode.y[i]/YSCALE - YOFFS;
  199.     }
  200.     if( nxy == 0 ) cpyvert( new );
  201.     else if( newx[nxy-1] == incode.x[0] && newy[nxy-1] == incode.y[0] )
  202.         cpyvert( new );
  203.     else
  204.     {
  205.         wrpoly( new );
  206.         cpyvert( new );
  207.     }
  208.     nvecs++;
  209. }    /* wrvect */
  210.